package algorithm.swordoff;


import java.util.Stack;

/**
 * 用两个栈实现队列
 * 与主站232题类似,但是不需要实现 peek 和 empty api
 */

public class SQ09 {
}


class CQueue {

    Stack<Integer> mainStack;
    Stack<Integer> minorStack;

    public CQueue() {
        mainStack = new Stack<>();
        minorStack = new Stack<>();
    }

    public void appendTail(int value) {
        mainStack.push(value);
    }

    public int deleteHead() {
        int pop = -1;
        // 需要多一步判断,因为测试用例会在队列为空的时候依然deleteHead,并且要求此时返回-1
        if (!mainStack.isEmpty() || !minorStack.isEmpty()) {
            // main留一个,其他全部倒腾到minor里,公共动作1
            leaveOneInMain();
            // 取出main剩下的这一个
            pop = mainStack.pop();
            // minor全部再倒腾回main,,公共动作2
            minor2Main();
        }

        return pop;
    }

    private void leaveOneInMain() {
        while (mainStack.size() > 1) {
            minorStack.push(mainStack.pop());
        }
    }

    private void minor2Main() {
        while (!minorStack.isEmpty()) {
            mainStack.push(minorStack.pop());
        }
    }

}